<?php
require '../database/sales.php';
require '../database/products.php';
$id=$_GET['id'];
$sales=new sales();
$data=$sales->showOne($id);
?>
<?php 
$pro=new products();
?>
<script>
  $(function() {
    $( ".datepicker" ).datepicker({
      showOn: "button",
      buttonImage: "images/iconCalendar.gif",
      buttonImageOnly: true,
       "dateFormat":"yy-mm-dd"
    });
  });
  </script>
  <script>
       $(document).ready(function(){
           $(".sale").blur(function(){
             $price=$("input[name='sa']").val();
             $id=$("input[name='pro_Id']").val();
             if($price == ""){
               document.getElementById("error").innerHTML="Khong duoc bo trong";             
             }else{
                   document.getElementById("error").innerHTML=""; 
                   if(isNaN($price)){
                       document.getElementById("error").innerHTML="Ban chua nhap so";  
                   }else{
                      if($id != ""){
                           $.ajax({
                           url:"modules/sales/priceajax.php",
                           type: 'POST',
                           data:"pro_id="+$id+"&km="+$price,
                           async: false,
                           success: function (data) {
                                $("#result").val(data);
                                $("#Km").show();
                           }
                       });
                      }
                   }
                   }
                
           });
       });
  </script>
  <script language='javascript'>
 function isNumberKey(evt)
 {
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
 return false;
 return true;
 }
 </script>
  <?php 
           $number_error="";
           $price_error="";
           if(isset($_POST['ok'])){               
               $pro_id=trim($_POST['pro_Id']);
               $pro_number=  trim($_POST['number']);
               $pro_price=  trim($_POST['sa']);
               $pro_gia=trim($_POST['giakhuyenmai']);
               $start=$_POST['start'];
               $end=$_POST['end'];
               $notice=$_POST['notice'];
               if($pro_id==null ||$pro_number == null||$pro_price==NULL||$pro_gia==null){
                   echo "Khong duoc bo trong phan co dau *";
               }else{
                   if($pro->isExist($pro_id))
                   {
                       if(is_numeric($pro_number)){
                           if($pro_price> 0&&$pro_price <=100){
                               if(($start == "" && $end != "")||($start != "" && $end == "")){
                                 echo 'Can nhap ca ngay bat dau hoac ngay ket thuc';
                               }else{
                                  if($sales->update($id,$pro_id, $pro_number, $pro_price, $pro_gia, $start, $end, $notice)){
                                      echo 'Thanh cong';
                                  }else echo 'Khong the cap nhat vao co so du lieu';  
                               }
                               
                           }else {
                              $price_error="Phai lon 0 va nho hon hoac bang 100"; 
                           }                           
                       }else $number_error="Khong phai la so";                       
                   }else  echo 'Khong ton tai Ma san pham nhu vay';
                   
               }
           }
  ?>
<h1>Them san pham khuyen mai </h1>
<form action="" method="post">
    <table>
        <tr>
            <td>Ma san pham :</td>
            <td><input  class="sale" type="text" name="pro_Id" id="proid" value="<?php 
            if(isset($_POST['pro_Id'])) echo $_POST['pro_Id'];
            echo $data['pro_Id'];
            ?>"><span style="color: red;">*</span></td>
        </tr>
        <tr>
            <td>So luong san pham khuyen mai :</td>
            <td><input type="text" name="number" onKeyPress="return isNumberKey(event)" value="<?php echo $data['sales_number'];?>"><span style="color: red;">*<?php echo $number_error;?></span></td>
        </tr>
        <tr>
            <td>So % khuyen mai :</td>
            <td><input type="text" name="sa" class="sale" value="<?php echo $data['sales_sales'];?>"><span id="error" style="color: red;">*<?php echo $price_error;?></span></td>
        </tr>
        <tr  id="Km">
            <td >Gia khuyen mai:</td>
            <td ><input type="text" readonly="true" value="<?php echo $data['priceSales'];?>" id="result" name="giakhuyenmai"/></td>
        </tr>
        <tr>
            <td>Ngay bat dau :</td>
            <td><input type="text" class="datepicker" name="start" value="<?php echo $data['start'];?>"></td>
        </tr>
        <tr>
            <td>Ngay ket thuc :</td>
            <td><input type="text" class="datepicker" name="end" value="<?php echo $data['end'];?>"></td>
        </tr>
        <tr>
        <td>Ghi chu :</td>
        <td><textarea name="notice" cols="10" rows="9"><?php echo $data['notice'];?></textarea></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Them vao" name="ok"></td>
        </tr>
    </table>
</form>
